#!/bin/bash


set -x

SERVER_WAN_NIC=eth6

UDP_PORT=6882


TUN_NIC=toto0

SERVER_ADDR=192.168.100.1
CLIENT_ADDR=192.168.100.2


setup_route_client()
{
	ifconfig $TUN_NIC $CLIENT_ADDR dstaddr $SERVER_ADDR # netmask 255.255.255.0

	grep -q -w rtvpn /etc/iproute2/rt_tables || echo "201 rtvpn" >> /etc/iproute2/rt_tables

	ip route add default via $SERVER_ADDR table rtvpn
	ip rule add from $CLIENT_ADDR table rtvpn
}


setup_route_server()
{
	
	ifconfig $TUN_NIC $SERVER_ADDR dstaddr $CLIENT_ADDR

	iptables -t nat -F

	iptables -t nat -A PREROUTING -i $SERVER_WAN_NIC -p udp --dport $UDP_PORT -j DNAT --to-destination $CLIENT_ADDR
	iptables -t nat -A POSTROUTING -o $SERVER_WAN_NIC -j MASQUERADE

	echo 1 > /proc/sys/net/ipv4/ip_forward
}


set -e


case $1 in
	1)
	setup_route_server
	;;
	2)
	setup_route_client
	;;
	*)
	echo "Usage: $0 1(server)|2(client)"
	exit 1
	;;
esac

exit 0

